package com.gexiaobao.pigeon.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.OrgDeviceInfoResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.CreateDeviceParams
import com.gexiaobao.pigeon.app.model.param.ModifyDeviceInfoParams
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Author      : hxw
 * @Date        : 2023/3/3 11:32
 * @Describe    :
 */
class RequestDeviceViewModel : BaseViewModel() {

    private var pageNo = 1
    private var pageSize = 20

    /**  设备列表  */
    var deviceList: MutableLiveData<ListDataUiState<OrgDeviceInfoResponse.ListBean>> = MutableLiveData()

    /** 给组织方添加设备 */
    var createDeviceResult: MutableLiveData<UiState> = MutableLiveData()

    /** 给组织方修改设备 */
    var updateDeviceResult: MutableLiveData<UiState> = MutableLiveData()

    /** 给组织方删除设备 */
    var deleteDeviceResult: MutableLiveData<UiState> = MutableLiveData()

    fun getMemberDeviceList(isRefresh: Boolean, entryName: String, shedNo: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getMemberDeviceList(pageNo, pageSize, shedNo, entryName) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            deviceList.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errMessage = it.msg,
                    listData = arrayListOf<OrgDeviceInfoResponse.ListBean>()
                )
            deviceList.value = listDataUiState
        })
    }

    /** 组织方添加设备信息 */
    fun createDevice(body: CreateDeviceParams) {
        request({ apiService.createMemberDevice(body) }, {
            val result = UiState(isSuccess = true)
            createDeviceResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            createDeviceResult.value = result
        })
    }

    /** 组织方修改设备信息 */
    fun updateMemberDevice(body: ModifyDeviceInfoParams) {
        request({ apiService.updateMemberDevice(body) }, {
            val result = UiState(isSuccess = true)
            updateDeviceResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            updateDeviceResult.value = result
        })
    }

    /** 组织方删除设备信息 */
    fun deleteMemberDevice(id: String) {
        request({ apiService.deleteMemberDevice(id) }, {
            val result = UiState(isSuccess = true)
            deleteDeviceResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            deleteDeviceResult.value = result
        })
    }

}